//
//  BaiduMobAdPrerollViewController.m
//  XAdSDKDevSample
//
//  Created by lishan04 on 16/5/5.
//  Copyright © 2016年 Baidu. All rights reserved.
//

#import "BaiduMobAdPrerollViewController.h"
#import <BaiduMobAdSDK/BaiduMobAdSetting.h>

@interface BaiduMobAdPrerollViewController ()
@property (nonatomic, retain) BaiduMobAdPreroll *prerollAd;
@property (nonatomic, retain) UIButton *button;
@end

@implementation BaiduMobAdPrerollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 330, 200, 30);
    button.backgroundColor = [UIColor grayColor];
    [button setTitle:@"点击加载视频贴片广告" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.hidden = NO;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(loadAd) forControlEvents:UIControlEventTouchUpInside];

    self.button = button;
    [self loadAd];
}

- (void)loadAd {
    self.button.userInteractionEnabled = NO;
    float statusHeight = 64;
    if (ISIPHONEX) {
        statusHeight = 84;
    }
    UIView *baseview = [[UIView alloc]initWithFrame:CGRectMake(10, statusHeight, 320, 240)];
    [self.view addSubview:baseview];
    [BaiduMobAdSetting sharedInstance].supportHttps = YES;

    self.prerollAd = [[BaiduMobAdPreroll alloc]init];
    self.prerollAd.publisherId = @"ccb60059";
    self.prerollAd.adId = @"2058633";//需要为视频贴片广告位
    self.prerollAd.renderBaseView = baseview;
    self.prerollAd.delegate = self;
    // 是否有倒计时和点击描述按钮
//    self.prerollAd.supportActImage = NO;
//    self.prerollAd.supportTimeLabel = NO;
    [self.prerollAd request];
}

- (void)dealloc {
    self.prerollAd.delegate = nil;
    self.prerollAd = nil;
}

- (BOOL) enableLocation {
    return YES;
}

- (void)didAdReady:(BaiduMobAdPreroll *)preroll {
    NSLog(@"didAdReady");
}

- (void)didAdFailed:(BaiduMobAdPreroll *)preroll withError:(BaiduMobFailReason) reason{
    NSLog(@"didAdFailed");
    [self dd];
}

- (void)didAdStart:(BaiduMobAdPreroll *)preroll {
    NSLog(@"didAdStart");
}

- (void)didAdFinish:(BaiduMobAdPreroll *)preroll {
    NSLog(@"didAdFinish");
    [self dd];
}

- (void)dd {
    self.button.userInteractionEnabled = YES;
    [self loadAd];
}

- (void)didAdClicked:(BaiduMobAdPreroll *)preroll {
    NSLog(@"didAdClicked");
}

- (void)didDismissLandingPage {
    NSLog(@"didDismissLandingPage");
    
}

@end
